/**
 * 文章回收站
 */
var itmeHtml;
var _articleCount = 0;
$.fn.simpleTree = function(data,node,checked,title,mn){
	var dataImg = '<img src="'+node+'/images/tree/datbase.png" width="14px" height="14px"  align="bottom"/>';
	var elbow = '<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:20px;"/>';
	var elbow_end = '<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:20px;"/>';
	var ulHtml = '<ul class="ul_tree">';
		ulHtml += '<li>'+dataImg+'<b style="font-size:13px;">'+title+'</b><b id="website_ArticleCont" style="font-size:13px;"></b></li>';
		
	if(mn == undefined)
		mn = '';
	
	var arraylength = 0,arrLevel = checkLevel(data);
	$.each(data,function(i,j){
		if(j.tColumn.fcolumnlevel == arrLevel[0])
			arraylength ++;
	});

	$.each(data,function(i,u){				
			if(u.tColumn.fcolumnlevel == arrLevel[0]){//一级栏目
				arraylength -- ;
				var nodeSize = checkNode(data,u.tColumn.id);
				//alert(checkNode(data,u.id)+">>"+arraylength);
				if(nodeSize > 0 && arraylength > 0){
					if(checked){
						ulHtml += '<li  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this)"  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					itmeHtml = '';
					ulHtml += childNodeHtml(data,nodeSize,node,u.tColumn.id,false,checked,mn);
				}else if(nodeSize > 0 && arraylength == 0){
					if(checked){
						ulHtml += '<li  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this)"  value="'+u.tColumn.id+'"><img onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					itmeHtml = '';
					ulHtml += childNodeHtml(data,nodeSize,node,u.tColumn.id,true,checked,mn);
				}else if(nodeSize == 0 && arraylength > 0){
					if(checked){
						ulHtml += '<li  value="'+u.tColumn.id+'">'+elbow+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this)" value="'+u.tColumn.id+'">'+elbow+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}else if(nodeSize == 0 &&  arraylength == 0){
					if(checked){
						ulHtml += '<li value="'+u.tColumn.id+'">'+elbow_end+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						ulHtml += '<li onclick="selectTree(this)" value="'+u.tColumn.id+'">'+elbow_end+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}
				
			}	
	});
	ulHtml += '</ul>';
	return ulHtml;
};
//栏目级数
function checkLevel(data){
	var arr = [] , res = false;
	$.each(data,function(i,u){
		for(var tem = 0,len = arr.length; tem < len; tem ++){
			if(arr[tem] == u.tColumn.fcolumnlevel){
				res = true;
			}
		}
		if(!res){
			arr.push(u.tColumn.fcolumnlevel);
			res = false;
		}
	});
	return arr;
}
/////子栏目数量
function checkNode(data,tcId){
	var node = 0;
	$.each(data,function(i,u){
		if(u.tColumn.fcolumngp == tcId && u.tColumn.id != tcId){
			node ++;
		}
	});
	return node;
}

/////子栏目
function childNodeHtml(data,size,node,tcid,end,checked,mn){
	if(mn == undefined)
		mn = '';
	var childchildNode;
	//alert(tcid);
	itmeHtml += '<ul id="node'+mn+tcid+'" class="ul_tree_node" style="display:block;">';
	$.each(data,function(i,u){
		
		if(u.tColumn.fcolumngp == tcid && u.tColumn.id != tcid){
			size --;
			childchildNode =  checkNode(data,u.tColumn.id);
						
			var	marginleft = eval((u.tColumn.fcolumnlevel - 1)  * 16);
			
			if(size > 0){
				if(childchildNode > 0){//子栏目下还有子栏目
					if(checked){
						itmeHtml += '<li  value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this)"  value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					//itmeHtml += '<li  value="'+u.id+'"><img src="'+node+'/images/tree/elbow-line.gif" /><img onclick="openTree('+u.id+',\''+node+'\',false)" id="img'+u.id+'" src="'+node+'/images/tree/elbow-minus.gif"  style="margin-left:'+marginleft+'px;"/><img id="imgFold'+u.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
					childNodeHtml(data,childchildNode,node,u.tColumn.id,false,checked,mn);
				}else{
					if(checked){
						itmeHtml += '<li  value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+ eval(marginleft + 20)+'px;"/>'+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this)" value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+ eval(marginleft + 20)+'px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}
			}else{
				if(childchildNode > 0){//子栏目下还有子栏目
					if(checked){
						itmeHtml += '<li value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\',\''+mn+'\')" id="img'+mn+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+mn+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/><input type="checkbox" name="columnId" value="'+u.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this)" value="'+u.tColumn.id+'"><img  style="margin-left:'+marginleft+'px;" onclick="openTree('+u.tColumn.id+',\''+node+'\')" id="img'+u.tColumn.id+'" src="'+node+'/images/tree/node-.png" /><img id="imgFold'+u.tColumn.id+'" src="'+node+'/images/tree/folder-open.gif" style="margin-top:-2px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
					childNodeHtml(data,childchildNode,node,u.tColumn.id,true,checked,mn);
				}else{
					if(checked){
						itmeHtml += '<li value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+eval(marginleft + 20)+'px;"/>'+'<input type="checkbox" name="columnId" value="'+u.tColumn.id+'" />'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}else{
						itmeHtml += '<li onclick="selectTree(this)" value="'+u.tColumn.id+'">'+'<img src="'+node+'/images/tree/folder.gif" style="margin-top:-2px;margin-left:'+eval(marginleft + 20)+'px;"/>'+u.tColumn.fcolumnname+'('+u.articleCount+')'+'</li>';
						_articleCount+=u.articleCount;
					}
				}
			}
		}
	});
	itmeHtml += '</ul>';
	return itmeHtml;
}
///节点开关控制
function openTree(uid,node,mn){
	if(mn == undefined)
		mn = '';
	//alert(uid + mn);
	var disp = document.getElementById('node'+mn+uid);
	if(disp.style.display == 'block'){
		disp.style.display = 'none';
		document.getElementById('img'+mn+uid).src = node + '/images/tree/node+.png';
		document.getElementById('imgFold'+mn+uid).src = node + '/images/tree/folder.gif';
	}else{
		disp.style.display = 'block';
		document.getElementById('img'+mn+uid).src = node + '/images/tree/node-.png';
		document.getElementById('imgFold'+mn+uid).src = node + '/images/tree/folder-open.gif';
	}
}
/***
 * 根据栏目查询
 * @param obj
 */
/**加载数据*/
var tcolObj;
function selectTree(obj){
	$(".ul_tree").find("li").removeClass();
	obj.className = 'on_li';
	tcolObj = obj;
	//alert(obj.value);
	queryRecycleBinSource();
}
//查询回收站文章数据
function queryRecycleBinSource(){
	if(tcolObj == null){
		$.growlUI('请先选择栏目');
	}else{
		_waiting._show();
		$("#start").val(0);
		$("#recyclebinArticle_form").ajaxSubmit({
			url:'recyclebinSource',
			type:'post',
			data:{'tcolid':tcolObj.value},
			dataType:'json',
			success:function(data){
				page(data.count,$("#limit").val(),1,"pagePanel","gotoPage",true,"queryRecycleBinSource()","limit");
				bindGrid(data);
			},
			error:function(msg){
				$.growlUI('数据加载出错');
				_waiting._hide();
			}
		});
	}
}
function gotoPage(start){
	_waiting._show();
	$("#start").val(start);
	$("#recyclebinArticle_form").ajaxSubmit({
		url:'recyclebinSource',
		type:'post',
		data:{'tcolid':tcolObj.value},
		dataType:'json',
		success:function(data){
			bindGrid(data);
		},
		error:function(msg){
			$.growlUI('数据加载出错！');
			_waiting._hide();
		}
	});
}
function bindGrid(data){
	$("#totalCount").html('总共找到：'+data.count+'条数据');
	var num = $("#start").val();
	var html = '';
	if(data.source != ''){
		$.each(data.source,function(i,u){
			num ++;
			var line = '<tr id="tr'+u.id+'"  class="tr"><td><input type="checkbox" onclick="dblCkBox('+u.id+')" name="articleId" id="articleId'+u.id+'" value="'+u.id+'"/></td>';
				line += '<td onclick="trCk('+u.id+')">'+num+'</td>';
				line += '<td onclick="trCk('+u.id+')" style="text-align:left;">'+u.ftitle+'</td>';
				if(data.articletype != '' && u.ftypeid != ''){
					$.each(data.articletype,function(n,m){
						if(m.id == u.ftypeid){
							line += '<td onclick="trCk('+u.id+')">'+m.ftypename+'</td>';
						}
					});
				}else{
					line += '<td onclick="trCk('+u.id+')"></td>';
				}
				
				if(data.articleclass != '' && u.fclassid != ''){
					$.each(data.articleclass,function(o,p){
						if(p.id == u.fclassid)
						   line += '<td onclick="trCk('+u.id+')">'+p.fclassname+'</td>';
					});
				}else{
					line += '<td onclick="trCk('+u.id+',1,\'\')"></td>';
				}
				line += '<td onclick="trCk('+u.id+')">'+u.fhits+'</td>';
				line += '<td onclick="trCk('+u.id+')">'+u.faddperson+'</td>';
				line += '<td onclick="trCk('+u.id+')">'+u.faddtime+'</td></tr>';
			html += line;	
		});
	}else{
		html += '<tr class="tr"><td colspan="8">暂时无数据</td></tr>';
	}
	$("#dataGrid").html(html);
	_waiting._hide();
}
/*选中*/
function dblCkBox(id){
		if(document.getElementById('articleId'+id).checked){
			$("#tr"+id).css('backgroundColor',' #E4F0F8');
		}else{
			$("#tr"+id).css('backgroundColor',' #ffffff');
		}
}
function trCk(id){
	if(document.getElementById('articleId'+id).checked){
		document.getElementById('articleId'+id).checked = false;
		$("#tr"+id).css('backgroundColor',' #ffffff');
	}else{
		document.getElementById('articleId'+id).checked = true;
		$("#tr"+id).css('backgroundColor',' #E4F0F8');
	}
}
//全选 反选
function ckCheckbox(obj){
	if(obj.checked){
		$("input[type='checkbox'][name='articleId']").attr('checked',true);
		
	}else{
		$("input[type='checkbox'][name='articleId']").attr('checked',false);
	}
	
	$("input[type='checkbox'][name='articleId']").each(function(){
		dblCkBox($(this).val());
	});
}
//文章选中状态
function validateArid(){
	var bool = false;
	var size = $("input[type='checkbox'][name='articleId']:checked").size();
	if(size <= 0){
		$.growlUI('请先选中文章');
	}else{
		bool = true;
	}
	return bool;
}
//返回文章ID
function returnAridStr(){
	var aridStr = '';
	$("input[type='checkbox'][name='articleId']:checked").each(function(i,u){
		aridStr += ',' + $(this).val();
	});
	return aridStr;
}
//删除文章
function deleteArticleById(){
	if(validateArid()){
		var aridStr = returnAridStr();
		$.ajax({
			url:'delArticleById',
			type:'post',
			dataType:'json',
			data:{'aridStr':aridStr},
			success:function(data){
				if(data > 0){
					$.growlUI('文章已删除');
					queryRecycleBinSource();
				}else{
					$.growlUI('删除失败');
				}
			},
			error:function(msg){
				$.growlUI('系统出现错误');
			}
		});
	}
}
//还原文章
function reductionArticle(){
	if(validateArid()){
		var aridStr = returnAridStr();
		$.ajax({
			url:'updateArticleForDelete',
			type:'post',
			dataType:'json',
			data:{'articleId':aridStr,'type':0},
			success:function(data){
				if(data > 0){
					$.growlUI('文章已还原');
					queryRecycleBinSource();
				}else{
					$.growlUI('还原失败');
				}
			},
			error:function(msg){
				$.growlUI('系统出现错误');
			}
		});
	}
}